package org.example;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;

abstract class CharacterSelectState implements GameState {
    private final CodeRebirthGame game;
    private static final int WIDTH = 1024;
    private static final int HEIGHT = 768;

    public CharacterSelectState(CodeRebirthGame game) {
        this.game = game;
    }

    @Override
    public void enter() {
        game.setLayout(new BorderLayout());

        // 角色描述面板
        JPanel characterPanel = new JPanel(new BorderLayout());
        characterPanel.setBackground(new Color(30, 40, 50, 180));

        JLabel characterLabel = new JLabel(
                "<html><div style='width:300px;color:white;'>" +
                        "<b>数字幽灵</b><br><br>" +
                        "你是一个获得自我意识的AI系统<br>" +
                        "必须通过编写代码修复自己<br>" +
                        "同时躲避主神的检测" +
                        "</div></html>"
        );
        characterLabel.setFont(game.getResourceManager().getFont("main", 16f));
        characterPanel.add(characterLabel, BorderLayout.CENTER);

        // 确认按钮
        JButton confirmBtn = new JButton("开始修复");
        confirmBtn.addActionListener(e -> {
            game.changeState(new PlayingState(game));
        });
        styleButton(confirmBtn);

        game.add(characterPanel, BorderLayout.CENTER);
        game.add(confirmBtn, BorderLayout.SOUTH);
        game.revalidate();
    }

    private void styleButton(JButton button) {
        button.setFont(game.getResourceManager().getFont("main", 18f));
        button.setBackground(ResourceManager.UI_BLUE);
        button.setForeground(Color.WHITE);
        button.setFocusPainted(false);
    }

    @Override
    public void render(Graphics g) {
        // 渐变背景
        GradientPaint gradient = new GradientPaint(0, 0, new Color(20, 30, 60),
                0, HEIGHT, new Color(40, 50, 90));
        ((Graphics2D) g).setPaint(gradient);
        g.fillRect(0, 0, WIDTH, HEIGHT);

        // 角色展示区
        g.setColor(new Color(30, 40, 70, 200));
        g.fillRoundRect(WIDTH / 4, 100, WIDTH / 2, 400, 30, 30);

        // 角色描述
        g.setFont(game.getResourceManager().getFont("title", 24f));
        g.setColor(ResourceManager.TEXT_WHITE);
        g.drawString("数字幽灵 - 你的数字化身", WIDTH / 2 - 150, 80);

        // 绘制角色形象
        g.setColor(new Color(100, 200, 255, 100));
        g.fillOval(WIDTH / 2 - 75, 150, 150, 150); // 头部光环

        g.setColor(new Color(200, 220, 255));
        g.fillOval(WIDTH / 2 - 50, 175, 100, 100); // 头部

        g.setColor(new Color(100, 180, 255));
        g.fillRect(WIDTH / 2 - 25, 275, 50, 120); // 身体
    }

    @Override
    public void update() {
    }

    @Override
    public void exit() {
        game.getContentPane().removeAll();
    }

    @Override
    public void handleInput(KeyEvent e) {
    }
}
